/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringReader;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.ws.WebServiceRef;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import service.GlobalWeather;

/**
 *
 * @author ADMIN
 */
@WebServlet(name = "getWeatherServlet1", urlPatterns = {"/getWeatherServlet1"})
public class getWeatherServlet1 extends HttpServlet {

    @WebServiceRef(wsdlLocation = "WEB-INF/wsdl/www.webservicex.net/globalweather.asmx.wsdl")
    private GlobalWeather service;

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            String s = getWeather(request.getParameter("city"), request.getParameter("country"));
            
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            InputSource is = new InputSource(new StringReader(s));
            Document doc = dBuilder.parse(is);
            
            request.setAttribute("Country", request.getParameter("country"));
            request.setAttribute("City", request.getParameter("city"));
            request.setAttribute("Location", doc.getElementsByTagName("Location").item(0).getTextContent());
            request.setAttribute("Time", doc.getElementsByTagName("Time").item(0).getTextContent());
            request.setAttribute("Wind", doc.getElementsByTagName("Wind").item(0).getTextContent());
            request.setAttribute("Visibility", doc.getElementsByTagName("Visibility").item(0).getTextContent());
            request.setAttribute("SkyConditions", doc.getElementsByTagName("SkyConditions").item(0).getTextContent());
            request.setAttribute("Temperature", doc.getElementsByTagName("Temperature").item(0).getTextContent());
            request.setAttribute("DewPoint", doc.getElementsByTagName("DewPoint").item(0).getTextContent());
            request.setAttribute("RelativeHumidity", doc.getElementsByTagName("RelativeHumidity").item(0).getTextContent());
            request.setAttribute("Pressure", doc.getElementsByTagName("Pressure").item(0).getTextContent());
            
            request.getRequestDispatcher("/success.jsp").forward(request, response);
        } catch (ParserConfigurationException | SAXException ex) {
            Logger.getLogger(getWeatherServlet1.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    private String getWeather(java.lang.String cityName, java.lang.String countryName) {
        // Note that the injected javax.xml.ws.Service reference as well as port objects are not thread safe.
        // If the calling of port operations may lead to race condition some synchronization is required.
        service.GlobalWeatherSoap port = service.getGlobalWeatherSoap();
        return port.getWeather(cityName, countryName);
    }


    
}
